﻿using System;
using System.Collections;
using System.Collections.Generic;
using Smart.Model;
using UnityEngine;

public class Player : Actor
{
    [SerializeField]
    float offsetY = 0.9f;

    float rotation = 0.0f;
    float targetRotation = 0.0f;

    [SerializeField]
    float rtSpeed = 360;

    Vector3 rs = Vector3.zero;

    public Quaternion Rotation
    {
        get
        {
            return transform.rotation;
        }
    }

    public void Attach()
    {
        if (this.AgentId >= 0)
        {
            //Vector3 p = NavMeshInterface.GetAgentPos(crowd, this.agentId);
            //p.y += agentOffset;
            //transform.position = p;
            //rotation = targetRotation = 0.0f;
            //transform.localRotation = Quaternion.Euler(0, rotation, 0);
        }
    }

    public virtual void Update()
    {
        //if (null != this.crowd && this.agentId >= 0)
        //{
        //    Vector3 p = NavMeshInterface.GetAgentPos(crowd, this.agentId);
        //    p.y += agentOffset;
        //    transform.position = p;
        //}

        //if (Mathf.Abs(rotation - targetRotation) > 1e-5)
        //{
        //    float v = targetRotation - rotation;
        //    if (v > 0)
        //    {
        //        rotation = rotation + Time.deltaTime * rtSpeed;
        //        if (rotation > targetRotation)
        //        {
        //            rotation = targetRotation;
        //        }
        //        rs.y = rotation;
        //        transform.localEulerAngles = rs;
        //    }
        //    else
        //    {
        //        rotation = rotation - Time.deltaTime * rtSpeed;
        //        if (rotation < targetRotation)
        //        {
        //            rotation = targetRotation;
        //        }
        //        rs.y = rotation;
        //        transform.localEulerAngles = rs;
        //    }
        //}
    }

    public void SetRotationY(float delta)
    {
        targetRotation += delta;
    }


    public override void OnCreate()
    {

    }

    public void RaceMoveVelocityClient(float x, float y, float z)
    {
        RaceLogic.ClientPosToRace(x, y, z, out var xx, out var yy, out var zz);
        GameEntry.RaceMgr.AgentMoveVelocity(AgentId, xx, yy, zz);
    }
    
    public void RaceMoveVelocity(long x, long y, long z)
    {
        GameEntry.RaceMgr.AgentMoveVelocity(AgentId, x, y, z);
    }

    public void RaceGetPosClient(out float x, out float y, out float z)
    {
        GameEntry.RaceMgr.GetAgentPos(AgentId, out var xx, out var yy, out var zz);
        RaceLogic.RacePosToClient(xx, yy, zz, out x, out y, out z);
    }
    
    public void RaceGetPos(out long x, out long y, out long z)
    {
        GameEntry.RaceMgr.GetAgentPos(AgentId, out x, out y, out z);
    }
    
    public void RaceSetPositionClient(float x, float y, float z)
    {
        RaceLogic.ClientPosToRace(x, y, z, out var xx, out var yy, out var zz);
        GameEntry.RaceMgr.SetAgentPos(AgentId, xx, yy, zz);
    }

    public void RaceSetPosition(long x, long y, long z)
    {
        GameEntry.RaceMgr.SetAgentPos(AgentId, x, y, z);
    }

    public void RaceStopMove()
    {
        GameEntry.RaceMgr.StopAgentMove(AgentId);
    }

    public void SetByRacePosition(long x, long y, long z)
    {
        RaceSetPosition(x, y, z);
        RaceLogic.RacePosToClient(x, y, z, out var xx, out var yy, out var zz);
        transform.position = new Vector3(xx, yy, zz);
    }

    public virtual void OnDestroy()
    {
        
    }
}